﻿using System;
using System.Collections.Generic;
using System.Text;
//using smsclictr.automation;
using Microsoft.SystemCenter.Orchestrator.Integration;
using System.Management;
using System.Globalization;
using sccmclictr.automation;
using sccmclictr.automation.schedule;

namespace OpalisAddOn.ServiceWindow
{
    /// <summary>
    /// Create a new ServiceWindow
    /// </summary>
    [Activity("NewServiceWindow", Description = "Create a new SCCM Service Window by creating a local policy...")]
    public class NewServiceWindow : IActivity
    {
        private String targetComputer;
        private String userName;
        private string password;
        private DateTime startDate;
        private int hourDuration;
        private Boolean isGMT = false;
        //private Guid serviceWindowGUID;

        public void Design(IActivityDesigner designer)
        {
            designer.AddInput("TargetComputer").WithComputerBrowser();
            designer.AddInput("UserName").NotRequired();
            designer.AddInput("Password").NotRequired().PasswordProtect();

            designer.AddInput("StartDate").NotRequired().WithDateTimeBrowser();
            designer.AddInput("HourDuration").WithDefaultValue(1);
            designer.AddInput("IsGMT").NotRequired().WithBooleanBrowser().WithDefaultValue(false);

            designer.AddOutput("ServiceWindowGUID").AsString().WithDescription("GUID of ServiceWindow");
        }

        public void Execute(IActivityRequest request, IActivityResponse response)
        {
            try
            {
                targetComputer = request.Inputs["TargetComputer"].AsString();
                userName = request.Inputs["UserName"].AsString();
                password = request.Inputs["Password"].AsString();

                SCCMAgent oClient;
                if (string.IsNullOrEmpty(userName))
                {
                    oClient = new SCCMAgent(targetComputer);
                }
                else
                {
                    oClient = new SCCMAgent(targetComputer, userName, password);
                }

                //response.Publish("ServiceWindowGUID", request.Inputs["StartDate"].AsString());

                if (request.Inputs.Contains("StartDate"))
                {
                    IFormatProvider culture = CultureInfo.CurrentCulture;
                    if(request.Inputs["StartDate"].AsString().Contains("AM") | request.Inputs["StartDate"].AsString().Contains("PM"))
                        culture = new CultureInfo("en-US", true);
                    startDate = DateTime.Parse(request.Inputs["StartDate"].AsString(), culture);
                }

                if (request.Inputs.Contains("IsGMT"))
                {
                    isGMT = Boolean.Parse(request.Inputs["IsGMT"].AsString());
                }

                if (request.Inputs.Contains("HourDuration"))
                {
                    hourDuration = int.Parse(request.Inputs["HourDuration"].AsString());
                }

                ScheduleDecoding.SMS_ST_NonRecurring oSched = new ScheduleDecoding.SMS_ST_NonRecurring();

                if (startDate.Year < 2000)
                {
                    startDate = DateTime.Now;
                }
                oSched.StartTime = startDate;
                oSched.IsGMT = isGMT;

                TimeSpan ts = new TimeSpan(hourDuration, 0, 0);
                oSched.MinuteDuration = ts.Minutes;
                oSched.HourDuration = ts.Hours;

                oSched.DayDuration = 0;

                string sGUID = oClient.Client.RequestedConfig.CreateServiceWindow(oSched.ScheduleID, 6);

                response.Publish("ServiceWindowGUID", sGUID.ToString());
            }
            catch(Exception ex)
            {
                throw new ActivityWarning("Error", ex);
            }
        }
    }
    /// <summary>
    /// Delete a Service Window based on the SW GUID
    /// </summary>
    [Activity("DeleteServiceWindow", Description = "Delete localy created Service Windows...")]
    public class DeleteServiceWindow
    {
        private String targetComputer;
        private String userName;
        private string password;
        private string serviceWindowGUID;

        [ActivityInput]
        public String TargetComputer
        {
            set { targetComputer = value; }
        }

        [ActivityInput(Optional = true)]
        public String UserName
        {
            set { userName = value; }
        }

        [ActivityInput(Optional = true, PasswordProtected = true)]
        public String Password
        {
            set { password = value; }
        }

        [ActivityInput]
        public String ServiceWindowGUID
        {
            set { serviceWindowGUID = value; }
        }

        [ActivityMethod]
        public void Invoke()
        {
            try
            {
                SCCMAgent oClient;
                if (string.IsNullOrEmpty(userName))
                {
                    oClient = new SCCMAgent(targetComputer);
                }
                else
                {
                    oClient = new SCCMAgent(targetComputer, userName, password);
                }

                if (string.IsNullOrEmpty(serviceWindowGUID))
                {
                    oClient.Client.RequestedConfig.DeleteServiceWindow(serviceWindowGUID);
                }

                oClient.disconnect();

            }
            catch
            {
                throw new ActivityWarning();
            }
        }
    }

}
